package com.amkai.mobile.client;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

import com.amkai.mobile.bean.organization.MobileBusinessGroup;
import com.amkai.mobile.client.common.SharedDataProvider;
import com.amkai.mobile.client.options.OptionsProvider;
import com.amkai.mobile.common.job.organization.MobileOrganizationJobResult;

public class Login extends Activity {

	private Spinner sp_be;
	private Spinner sp_bg;
	MobileOrganizationJobResult mojr;
	Intent appointments;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// final Handler handler = new Handler() {
		// @Override
		// public void handleMessage(Message msg) {
		// // TODO Auto-generated method stub
		// Exception e = null;
		// super.handleMessage(msg);
		// if (msg.getData() != null) {
		// Bundle b = msg.getData();
		// e = (Exception) b.get("exception");
		// AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
		// builder.setMessage(e.toString()).setCancelable(false).setPositiveButton("Yes",
		// new DialogInterface.OnClickListener() {
		// public void onClick(DialogInterface dialog, int id) {
		// Activity a = Login.this.getParent();
		// Login.this.finish();
		// a.finish();
		// }
		// }).setNegativeButton("No", new DialogInterface.OnClickListener() {
		// public void onClick(DialogInterface dialog, int id) {
		// dialog.cancel();
		// }
		// });
		// AlertDialog alert = builder.create();
		// alert.show();
		// }
		//
		// }
		//
		// };

		setContentView(R.layout.login);
		sp_be = (Spinner) findViewById(R.id.sp_be);
		sp_bg = (Spinner) findViewById(R.id.sp_bg);
		// Bundle b = getIntent().getExtras();
		try {
			mojr = SharedDataProvider.getInstance().getMojr(this);
		} catch (Exception e) {
			Toast.makeText(this, "Conection Refused", Toast.LENGTH_SHORT).show();
			AeMobile.startMain(this, true);
		}
		if (mojr == null) {
			Toast.makeText(this, "Conection Refused", Toast.LENGTH_SHORT).show();
			AeMobile.startMain(this, true);
		}
		loadCombos();
		Button btn_login = (Button) findViewById(R.id.btn_login);

		btn_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Bundle b = new Bundle();
				MobileBusinessGroup mobileBusinessGroup = mojr.businessGroups.get(sp_bg.getSelectedItemPosition());

				b.putLong("department", mobileBusinessGroup.businessEntities.get(sp_be.getSelectedItemPosition()).id);
				if (SharedDataProvider.getInstance().getPassportId(Login.this) == null) {
					Toast.makeText(Login.this, "Connection refused! Please try AGAIN!", Toast.LENGTH_SHORT).show();
				
					AeMobile.startMain(Login.this, true);
					finish();
				} else {
					b.putLong("passport", SharedDataProvider.getInstance().getPassportId(Login.this));
					appointments = new Intent(Login.this, Appointments.class);
					appointments.putExtras(b);

					startActivity(appointments);
				}
			}

		});

	}

	private void loadCombos() {

		String[] bg_names = new String[mojr.businessGroups.size()];

		for (int i = 0; i < mojr.businessGroups.size(); i++) {
			bg_names[i] = mojr.businessGroups.get(i).name;
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, bg_names);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_bg.setAdapter(adapter);

		sp_bg.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				MobileBusinessGroup mbg = mojr.businessGroups.get(arg2);
				loadBeCombo(mbg);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		loadBeCombo(mojr.businessGroups.get(0));

	}

	private void loadBeCombo(MobileBusinessGroup mbg) {

		if (mbg.businessEntities != null) {

			String[] be_names = new String[mbg.businessEntities.size()];
			for (int i = 0; i < mbg.businessEntities.size(); i++) {
				be_names[i] = mbg.businessEntities.get(i).name;
			}
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, be_names);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			sp_be.setAdapter(adapter);

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return OptionsProvider.onCreateOptionsMenu(this, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return OptionsProvider.onOptionsItemSelected(this, item);
	}

}
